<?php

/**
 * sfWidgetFormInputFilePhEditable 
 * File widget with javascript for creation thumbs
 * 
 * @package    phImageBuilder
 * @subpackage widget
 * @author     Samusevich Alexander <archer@fishartis.com>
 * @version    1.0 2011-11-14 14:27 archer
 */
class sfWidgetFormInputImageBuilder extends sfWidgetFormInputFileEditable
{
   /**
   * Constructor.
   *
   * Available options:
   *
   *  * file_src:     The current image web source path (required)
   *  * edit_mode:    A Boolean: true to enabled edit mode, false otherwise
   *  * with_delete:  Whether to add a delete checkbox or not
   *  * delete_label: The delete label used by the template
   *  * thumbs:       Array of thumb parameters (required)
   *                    * width - thumb width
   *                    * height - thumb height
   *                    * method - creation method (procesing crop only)
   *                    * ext - thumb file extension
   *   
   *   * model:       Model class name (for callback)
   *   * callback:    Url for ajax crop handler. 
   *                  Avaible parameters:
   *                    * name - thumb name
   *                    * x - x offset
   *                    * y - y offset
   *                    * model - model class name
   * 
   * In edit mode, this widget renders an additional widget named after the
   * file upload widget with a "_delete" suffix. So, when creating a form,
   * don't forget to add a validator for this additional field.
   *
   * @param array $options     An array of options
   * @param array $attributes  An array of default HTML attributes
   *
   * @see sfWidgetFormInputFileEditable
   */
    protected function configure($options = array(), $attributes = array())
    {
        parent::configure($options, $attributes);
        
        // Add new required options
        $this->addRequiredOption('thumbs');
        $this->addRequiredOption('callback');
    }
    
    public function render($name, $value = null, $attributes = array(), $errors = array())
    {
        // include js and css for widget
        sfContext::getInstance()->getResponse()->addStylesheet('/phImageBuilderPlugin/css/imgareaselect-default.css');
        sfContext::getInstance()->getResponse()->addStylesheet('/phImageBuilderPlugin/css/styles.css');
        
        sfContext::getInstance()->getResponse()->addJavascript('/phImageBuilderPlugin/js/jquery.imgareaselect.js'); 
        sfContext::getInstance()->getResponse()->addJavascript('/phImageBuilderPlugin/js/script.js'); 
        
        $filepath = sfConfig::get('sf_web_dir') . $this->options['file_src'];
        if( function_exists('mime_content_type') && file_exists( $filepath ) )
        {
            $mime = mime_content_type( $filepath );    
        }
        
        $filesize = '';
        if( file_exists( $filepath ) )
        {
            $filesize = number_format( filesize($filepath) / 1024 / 1024, 2 );
            $filesize = ($filesize < 1) ? ($filesize * 100) . ' Kb' : $filesize . ' Mb';
        }
                            
        $sTemplate = '<div class="phimagebuilder_widget_c" callback="'.$this->options['callback'].'">' .
                        '<div rel="phimagebuilder">
                            %file%<br />%input%<br />%delete% %delete_label%
                            <br />
                            Размер файла: '.$filesize. ( isset($mime) ? ' ('.$mime.')' : '' ) .
                        '</div>' .
                            '<div class="phimagebuilder_thumb_container">';
                    
        $i=0;             
        foreach( $this->options['thumbs'] as $sKey => $aThumb )
        {
            // Checkedit mode
            if( !$this->options['edit_mode'] ) continue;
            
            // Exclude not croped preview
            if( !isset( $aThumb['method'] ) || $aThumb['method'] != 'crop' ) continue;
            
            $aThumb['ext'] = (isset($aThumb['ext']) && $aThumb['ext']) ? $aThumb['ext'] : '.' . $sKey;
            
            $sTemplate .=  "<div thumbIndex='".(++$i)."'>
                                <div class='phimagebuilder_label_size'>".$aThumb['width']."x".$aThumb['height']."</div>
                                <div class='phimagebuilder_label_apply'>Принять</div>
                                <div class='phimagebuilder_label_discard'>Отмена</div>
                                <div class='phimagebuilder_label_border'>
                                    <img thumbIndex='".($i)."' realWidth='".$aThumb['width']."' realHeight='".$aThumb['height']."' thumbName='".$sKey."' src='".$this->getOption('file_src') . $aThumb['ext'] ."' />
                                </div>
                            </div>";    
        }
        
        $sTemplate .= '</div><div style="clear: both;"></div></div>';
        
        // Set widget type and template
        $this->setOption('is_image', true);
        $this->setOption('template', $sTemplate);
        
        // Render parent image widget
        $input = parent::render($name, $value, $attributes, $errors); 
        
        return $input;
    }
}

?>